//
//  GDBusStatsViewController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/11.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDBusStatsViewController.h"
#import "MBProgressHUD+MJ.h"
#import "GDTansitionModelDateOperation.h"

@interface GDBusStatsViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) GDBusStationStatsModel * statsModel;
@end

@implementation GDBusStatsViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithTitle:@"返回上一级" style:UIBarButtonItemStylePlain target:self action:@selector(backItemClick)];
    self.navigationItem.leftBarButtonItem = barButton;
}

-(void) backItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    [MBProgressHUD showMessage:@"正在查询,请稍后..." toView:self.view];
    /**
     *  触发查询按钮后，获取到公交站点信息
     *
     */
    __weak typeof (self)selfDG = self; // 弱引用
    [GDTansitionModelDateOperation initDateWithBusStationStats:self success:^(GDBusStationStatsModel *model) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showSuccess:@"查询成功" toView:self.view];
        if (model.lines.count != 0) {
               [selfDG updateUI:model];
        }else{
            [self.navigationController popViewControllerAnimated:YES];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:[NSString stringWithFormat:@"%@",error] toView:self.view];
    }];

}

#pragma mark 更新UI  转到别的控制器中更新UI
-(void) updateUI:(GDBusStationStatsModel *) model{
    
    self.statsModel = model;
    // 当前的站点为
    UILabel * nowStatsLabel  = [[UILabel alloc] initWithFrame:CGRectMake(GDScreenSize.size.width* 0.1, GDScreenSize.size.height * 0.1, 320, 30)];
    nowStatsLabel.font = [UIFont systemFontOfSize:17];
    nowStatsLabel.textColor = [UIColor redColor];
    nowStatsLabel.text = [NSString stringWithFormat:@"经过站点:%@ 的公交有:",self.statsModel._id];
    [self.view addSubview:nowStatsLabel];
    // 经过的站点
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(GDMargin, CGRectGetMaxY(nowStatsLabel.frame), GDScreenSize.size.width - GDMargin , GDScreenSize.size.height  * 0.6) style:UITableViewStylePlain ];
    tableView.backgroundColor = [UIColor clearColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.statsModel.lines.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * ID = @"cell";
    UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell   = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = self.statsModel.lines[indexPath.row];
    return cell;
    
}
@end
